/*
 * Copyright (c) 2017, Li Bo/Alex Lee All rights reserved.
 * 
 * http://www.libo4dream.club/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package com.hyts.bmsp.dao.dictionary;

import java.util.LinkedList;
import java.util.List;


import com.alibaba.druid.util.StringUtils;
import com.hyts.bmsp.model.dictionary.DictionaryTypeModel;
import com.hyts.bmsp.model.dictionary.DictionaryTypeQueryDTO;
import com.hyts.bmsp.model.dictionary.DictionaryTypeViewDTO;
import com.hyts.bmsp.dao.AbstractDBDAO;
import com.hyts.bmsp.tool.JFinalUtil;
import com.jfinal.plugin.activerecord.Page;

/**
 * @ClassName: OneLevelDictionaryDAO
 * @Description: 数据字典类型数据数据访问层
 * @author: LiBo/Alex Lee
 * @date: 2017年9月23日 下午6:39:15
 * @email: alexlibochn@china-pui.com.cn/alex.link@foxmail.com
 */
public class lDictionaryTypeDAO implements AbstractDBDAO<DictionaryTypeModel,DictionaryTypeQueryDTO,DictionaryTypeViewDTO> {

	
	/* (non Javadoc)
	 * @Title: queryPOListByPage
	 * @Description: 查询信息列表
	 * @param dto
	 * @return
	 * @see com.alex.jfinal.base.dao.AbstractDBDAO#queryPOListByPage(java.lang.Object)
	 */
	@Override
	public Page<DictionaryTypeModel> queryPOListByPage(
			DictionaryTypeQueryDTO dto) {
		String sqlPrefix = "SELECT * ";
		List<Object> params = new LinkedList<Object>();
		StringBuilder sqlSubfix = new StringBuilder("FROM tbl_dictionary_type WHERE 1=1");
		if(!StringUtils.isEmpty(dto.getDicCode()))
		{
			sqlSubfix.append("AND DIC_CODE = ?");
			params.add(dto.getDicCode());
		}
		if(!StringUtils.isEmpty(dto.getDicName()))
		{
			sqlSubfix.append("AND DIC_NAME LIKE ?");
			params.add("%"+dto.getDicName()+"%");
		}
		if(dto.getOrderNo() != null)
		{
			sqlSubfix.append("AND ORDER_NO = ?");
			params.add(dto.getOrderNo());
		}
		sqlSubfix.append(" order by order_no");
		Page<DictionaryTypeModel> pageInfo = DictionaryTypeModel.dictModel.paginate(dto.getPageNo(), dto.getPageSize(), sqlPrefix, sqlSubfix.toString(), params.toArray());
		return pageInfo;
	}

	/* (non Javadoc)
	 * @Title: loadPO
	 * @Description: 加载基本信息
	 * @param dto
	 * @return
	 * @see com.alex.jfinal.base.dao.AbstractDBDAO#loadPO(java.lang.Object)
	 */
	@Override
	public DictionaryTypeModel loadPO(DictionaryTypeQueryDTO dto) {
		return DictionaryTypeModel.dictModel.findById(dto.getId());
	}

	/* (non Javadoc)
	 * @Title: editPO
	 * @Description: 编辑操作基本信息
	 * @param dto
	 * @return
	 * @see com.alex.jfinal.base.dao.AbstractDBDAO#editPO(java.lang.Object)
	 */
	@Override
	public boolean editPO(DictionaryTypeQueryDTO dto) {
	    DictionaryTypeModel model = DictionaryTypeModel.dictModel.findById(dto.getId());
	    try {
			return JFinalUtil.daoUpdateSetHandler(model,dto, DictionaryTypeModel.class).update();
		} catch (IllegalArgumentException | IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	/* (non Javadoc)
	 * @Title: savePO
	 * @Description: 保存新增基本信息
	 * @param dto
	 * @return
	 * @see com.alex.jfinal.base.dao.AbstractDBDAO#savePO(java.lang.Object)
	 */
	@Override
	public boolean savePO(DictionaryTypeQueryDTO dto) {
		DictionaryTypeModel model = new DictionaryTypeModel();
		try {
			return JFinalUtil.daoUpdateSetHandler(model,dto, DictionaryTypeModel.class).save();
		} catch (IllegalArgumentException | IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	/* (non Javadoc)
	 * @Title: deletePO
	 * @Description: 删除基本数据信息
	 * @param dto
	 * @return
	 * @see com.alex.jfinal.base.dao.AbstractDBDAO#deletePO(java.lang.Object)
	 */
	@Override
	public boolean deletePO(DictionaryTypeQueryDTO dto) {
		return DictionaryTypeModel.dictModel.deleteById(dto.getId());
	}

	

}
